package HLLib.base;

import HLCode.HLClass;
import HLCode.HLClassManager;
import HLCode.HLClassManager_H;
import HLCode.HLLibObject;

public class HLCharList extends HLLibObject {
    public HLClass GetClass(HLClassManager classManager) {
        return classManager.GetLibClass(HLClassManager_H.CLASS_GROUP_TYPE_LIB_BASE, HLClassManager_H.LIB_CLASS_BASE_CharList);
    }

    public char[] items;

    public HLCharList() {
        items = new char[0];
    }

    public HLCharList(char[] chars) {
        items = chars;
    }

    public HLCharList(int count) {
        items = new char[count];
    }

    public void SetCount(int count) {
        if (items.length < count) {
            Resize(items.length, count - items.length);
        } else if (items.length > count) {
            Resize(count, count - items.length);
        }
    }

    public void Add(int item) {
        Resize(items.length, 1);
        items[items.length - 1] = (char) item;
    }

    public void AddRange(HLCharList charList) {
        InsertRange(items.length, charList);
    }

    public void Clear() {
        items = new char[0];
    }

    public int Count() {
        return items.length;
    }

    public char GetItem(int index) {
        return items[index];
    }

    public void SetItem(int index, int item) {
        items[index] = (char) item;
    }

    public void Insert(int index, int item) {
        Resize(index, 1);
        items[index] = (char) item;
    }

    public void InsertRange(int index, HLCharList charList) {
        Resize(index, charList.items.length);
        System.arraycopy(charList.items, 0, items, index, charList.items.length);
    }

    public void CopyAll(int toIndex, HLCharList charList) {
        System.arraycopy(charList.items, 0, items, toIndex, charList.items.length);
    }

    public void Copy(int toIndex, HLCharList charList, int start, int length) {
        System.arraycopy(charList.items, start, items, toIndex, length);
    }

    public void RemoveAt(int index) {
        Resize(index, -1);
    }

    public void Resize(int index, int count) {
        items = Resize(items, index, count);
    }

    static char[] Resize(char[] items, int index, int count) {
        int len = items.length;
        char[] newItems = new char[len + count];
        System.arraycopy(items, 0, newItems, 0, index);
        if (count > 0) {
            System.arraycopy(items, index, newItems, index + count, len - index);
        } else {
            System.arraycopy(items, index - count, newItems, index, len + count - index);
        }

        return newItems;
    }
}
